﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace MobileCloneSite
{
    public partial class MainPage : System.Web.UI.Page
    {
        private List<Producer> producers;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                // This is not the first load of the page,
                // so we should skip the data binding
                return;
            }

            this.producers = Producer.GetSampleList();
            var producerNames = from producer in producers
                                select producer.Name;

            this.ProducerList.DataSource = producerNames;
            this.ProducerList.DataBind();
            this.ProducerList.SelectedIndex = 0;

            LoadProducerModels();

            this.ExtrasCheckBoxList.DataSource = GetExtras();
            this.ExtrasCheckBoxList.DataBind();

            this.EngineRadioButtonList.DataSource = GetEngines();
            this.EngineRadioButtonList.DataBind();
        }

        protected void SelectProducer(object sender, EventArgs e)
        {
            LoadProducerModels();
        }

        protected void ShowResults(object sender, EventArgs e)
        {
            string producer = this.ProducerList.SelectedValue;
            string model = this.ModelsList.SelectedValue;
            List<string> extras = new List<string>();
            foreach (ListItem extra in this.ExtrasCheckBoxList.Items)
            {
                if (extra.Selected)
                {
                    extras.Add(extra.Value);
                }
            }

            string engine = this.EngineRadioButtonList.SelectedValue;

            this.Results.Text = "Producer: " + producer + " Model: " + model + "<br/>" +
                                "Extras: <br/>";
            foreach (string extra in extras)
            {
                this.Results.Text += extra + "<br/>";
            }

            this.Results.Text += "Engine: " + engine;
        }

        private void LoadProducerModels()
        {
            producers = Producer.GetSampleList();
            string selectedProducer = this.ProducerList.SelectedValue;

            var models = producers.FirstOrDefault(p => p.Name == selectedProducer).Models;
            this.ModelsList.DataSource = models;
            this.ModelsList.DataBind();
        }

        private string[] GetExtras()
        {
            return new string[]
            {
                "Climatronic", "ElMirrors", "Alarm", "Gas device", "ElWindows",
                "Bord computer", "dvd", "cd", "GPS",
                "Hit", "Crashed", "Tunings", "Nitrous",
                "New",  "Used", "Tinted",
            };
        }

        private string[] GetEngines()
        {
            return new string[]
            {
                "Diesel", "Gas", "Hybrid", "Electrical"
            };
        }
    }
}